package edu.ycxy.poetry.service.serviceImpl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.ycxy.poetry.entity.Writer;
import edu.ycxy.poetry.mapper.WriterMapper;
import edu.ycxy.poetry.service.IWriterService;
import edu.ycxy.poetry.vo.AddPoetsVo;
import edu.ycxy.poetry.vo.UpdatePoetVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@Service
public class WriterServiceImpl extends ServiceImpl<WriterMapper, Writer> implements IWriterService {

    @Autowired
    private WriterMapper writerMapper;

    @Override
    public Writer findWriter(String WriterName) {
        return writerMapper.findByName(WriterName);
    }

    //查询作者的信息
    //使用mybatis-plus中的pageHelper插件进行分页
    @Override
    public PageInfo<Writer> selectListAuthor(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Writer> writers = writerMapper.selectListAuthor();
        PageInfo pageInfo = new PageInfo<>(writers);
        return pageInfo;
    }

    //根据作者的朝代信息查询相关的作者
    @Override
    public PageInfo<Writer> selectListByName(String DyName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Writer> writers = writerMapper.selectListByName(DyName);
        PageInfo pageInfo = new PageInfo(writers);
        return pageInfo;
    }

    // 添加诗人的详细信息
    @Override
    public Integer addPoets(AddPoetsVo addPoetsVo) {
        // 根据作者名查询是否存在该作者
        Writer writer = writerMapper.findByName(addPoetsVo.getName());
        // 说明该作者名存在
        if (writer != null){
            return 222;
        }else {
            writerMapper.addPoet(addPoetsVo);
            return 111;
        }
    }

    /**
     * 模糊查询作者的信息
     * @param name  模糊查询条件
     * @param pageNum 分页中的页码
     * @param pageSize 分页中的每页显示条数
     * @return pageInfo
     */
    @Override
    public PageInfo<Writer> findListByName(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Writer> listByName = writerMapper.findListByName(name);
        PageInfo pageInfo = new PageInfo(listByName);
        return pageInfo;
    }

    @Override
    public void deleteById(Integer id) {
        writerMapper.deleteById(id);
    }

    @Override
    public Writer findOneById(Integer id) {
        return writerMapper.findOneById(id);
    }

    @Override
    public Integer updatePoet(UpdatePoetVo updatePoetVo) {
        return  writerMapper.updatePoet(updatePoetVo);
    }

}
